var sortData;
var sortWindow;
function handleSort(tp, title) {
	// var url = context + "/system/group.do?method=sort&tp=" + tp + "&id=" +
	// id;
	// dialog(url, true, title, 400, 350);
	initWindow();
	sortWindow = dhxWins.createWindow("sortWindow", 20, 30, 400, 350);
	sortWindow.setText("排列顺序设置");
	mygrid = sortWindow.attachGrid();
	mygrid.setImagePath(context + "/resources/dhtmlx/codebase/imgs/");
	mygrid.setHeader("姓名, 登录ID, 性别, 部门, 职务, 级别");
	mygrid.setInitWidths("200,100,100,200,100,100");
	mygrid.setColAlign("left,left,center,left,left, left");
	mygrid.setColTypes("link,ed,ed,ed,ed,ed");
	mygrid.setColSorting("str,str,str,str,str,str");
	mygrid.setEditable(false);
	mygrid.init();
	mygrid.enableLightMouseNavigation(true);
	mygrid.load(data, "json");

}

function handleAppend(pid) {
	window.location.href = context + "/system/group.do?method=append&pid="
			+ $("gid").value;
}

function handleUpdate() {
	if (confirm("确定保存")) {
		if (document.form1.branch.checked) {
			createHidden("gbranch", "1");
			paramXML("gbranch", "1");
		} else {
			createHidden("gbranch", "0");
			paramXML("gbranch", "0");
		}

		paramXML("gid", $("gid").value);
		paramXML("gname", $("gname").value);

		var url = context + "/system/group.do?method=update";

		var result = sendRequest(url);

		if (result == 1) {
			alert("修改成功");
			parent.rename($("gname").value);
		} else {
			alert("修改失败");
		}
		// document.form1.action = context + "/system/group.do?method=update";
		// document.form1.submit();
	}
}

function handleDelete(id) {
	if (confirm("确定删除")) {
		document.form1.action = context + "/system/group.do?method=delete";
		document.form1.submit();
	}
}

function init(notroot, haschildren, gid) {
	var tabbar = new dhtmlXTabBar("a_tabbar", "top");
	tabbar.setImagePath(context + "/resources/dhtmlx/codebase/imgs/");
	tabbar.enableAutoReSize(true);
	tabbar.addTab("a1", "组信息编辑", "100px");
	tabbar.setTabActive("a1");

	// var toolbar = new dhtmlXToolbarObject("toolbarObj");
	var toolbar = tabbar.cells("a1").attachToolbar();
	toolbar.setIconsPath(context
			+ "/resources/dhtmlx/codebase/imgs/dhxtoolbar_dhx_imgs/");
	if (notroot) {
		toolbar.addButton("save", 1, "提交修改", "save.gif", "save_dis.gif");
	}
	toolbar.addButton("appendchild", 2, "添加子组", "new.gif", "new_dis.gif");
	toolbar.addButton("sortgroup", 3, "子组排序", "redo.gif", "redo_dis.gif");
	toolbar.addButton("sortuser", 4, "用户排序", "redo.gif", "redo_dis.gif");
	if (notroot && !haschildren) {
		toolbar.addButton("delete", 5, "删除", "cut.gif", "cut_dis.gif");
	}

	toolbar.attachEvent("onClick", function(id) {
				if (id == "save") {
					handleUpdate();
				} else if (id == "appendchild") {
					handleAppend();
				} else if (id == "sortgroup") {
					handleSort('group', '组内分组排序');
				} else if (id == "sortuser") {
					handleSort('user', '组内用户排序');
				} else if (id == "delete") {
					handleDelete();
				}
			});
	var myForm = new dhtmlXForm("form1");
	// tabbar.cells("a1").attachObject(myForm);
	tabbar.setContent("a1", "content");
}